package com.shiguiwu.springmybatis.designpattern.composite.scene;

import cn.hutool.json.JSONUtil;
import com.shiguiwu.springmybatis.designpattern.composite.scene.aggregates.TreeRich;
import com.shiguiwu.springmybatis.designpattern.composite.scene.engine.IEngine;
import com.shiguiwu.springmybatis.designpattern.composite.scene.engine.impl.TreeEngineHandle;
import com.shiguiwu.springmybatis.designpattern.composite.scene.vo.EngineResult;
import com.shiguiwu.springmybatis.designpattern.composite.scene.vo.TreeNode;
import com.shiguiwu.springmybatis.designpattern.composite.scene.vo.TreeNodeLink;
import com.shiguiwu.springmybatis.designpattern.composite.scene.vo.TreeRoot;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 根本不不要搭建微服务
 * @author: stone
 * @date: Created by 2021/8/24 17:07
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.designpattern.composite.scene
 */
@Slf4j
public class EngineController {


    public String process(final String userId, final String userSex, final int userAge) {
        log.info("使用if-else实现的需求。。。。。userId：{} userSex：{} userAge：{}", userId, userSex, userAge);
        if ("man".equals(userSex)) {
            if (userAge < 25) {
                return "果实A";
            }
            if (userAge >= 25) {
                return "果实B";
            }
        }

        if ("woman".equals(userSex)) {
            if (userAge < 25) {
                return "果实C";
            }
            if (userAge >= 25) {
                return "果实D";
            }
        }
        return null;
    }

    public String processPlus() {

        //节点：1
        TreeNode treeNode_01 = new TreeNode();
        treeNode_01.setTreeId(10001L);
        treeNode_01.setTreeNodeId(1L);
        treeNode_01.setNodeValue(null);
        treeNode_01.setNodeType(1);
        treeNode_01.setRuleKey("userGender");
        treeNode_01.setRuleDesc("用户性别[男/女]");

        //连接 1 -> 11

        TreeNodeLink treeNodeLink_11 = new TreeNodeLink();
        treeNodeLink_11.setNodeIdFrom(1L);
        treeNodeLink_11.setNodeIdTo(11L);
        treeNodeLink_11.setRuleLimitType(1);
        treeNodeLink_11.setRuleLimitValue("man");


        //1 ->12

        TreeNodeLink treeNodeLink_12 = new TreeNodeLink();
        treeNodeLink_12.setNodeIdFrom(1L);
        treeNodeLink_12.setNodeIdTo(12L);
        treeNodeLink_12.setRuleLimitType(1);
        treeNodeLink_12.setRuleLimitValue("woman");
        //组装list
        List<TreeNodeLink> treeNodeLinkList_1 = new ArrayList<>();
        treeNodeLinkList_1.add(treeNodeLink_11);
        treeNodeLinkList_1.add(treeNodeLink_12);
        //将连接放到节点中
        treeNode_01.setTreeNodeLinkList(treeNodeLinkList_1);

        //=====================================================


        //节点：11
        TreeNode treeNode_11 = new TreeNode();
        treeNode_11.setTreeId(10001L);
        treeNode_11.setTreeNodeId(11L);
        treeNode_11.setNodeValue(null);
        treeNode_11.setNodeType(1);
        treeNode_11.setRuleKey("userAge");
        treeNode_11.setRuleDesc("用户年龄");

        //连接 11 -> 111

        TreeNodeLink treeNodeLink_111 = new TreeNodeLink();
        treeNodeLink_111.setNodeIdFrom(11L);
        treeNodeLink_111.setNodeIdTo(111L);
        treeNodeLink_111.setRuleLimitType(3);
        treeNodeLink_111.setRuleLimitValue("25");


        //11 ->112

        TreeNodeLink treeNodeLink_112 = new TreeNodeLink();
        treeNodeLink_112.setNodeIdFrom(11L);
        treeNodeLink_112.setNodeIdTo(112L);
        treeNodeLink_112.setRuleLimitType(4);
        treeNodeLink_112.setRuleLimitValue("25");
        //组装list
        List<TreeNodeLink> treeNodeLinkList_11 = new ArrayList<>();
        treeNodeLinkList_11.add(treeNodeLink_111);
        treeNodeLinkList_11.add(treeNodeLink_112);
        //将连接放到节点中
        treeNode_11.setTreeNodeLinkList(treeNodeLinkList_11);

        //==================================================

        //节点：12
        TreeNode treeNode_12 = new TreeNode();
        treeNode_12.setTreeId(10001L);
        treeNode_12.setTreeNodeId(12L);
        treeNode_12.setNodeValue(null);
        treeNode_12.setNodeType(1);
        treeNode_12.setRuleKey("userAge");
        treeNode_12.setRuleDesc("用户年龄");

        //连接 12 -> 121

        TreeNodeLink treeNodeLink_121 = new TreeNodeLink();
        treeNodeLink_121.setNodeIdFrom(12L);
        treeNodeLink_121.setNodeIdTo(121L);
        treeNodeLink_121.setRuleLimitType(3);
        treeNodeLink_121.setRuleLimitValue("25");


        //12 ->112

        TreeNodeLink treeNodeLink_122 = new TreeNodeLink();
        treeNodeLink_122.setNodeIdFrom(12L);
        treeNodeLink_122.setNodeIdTo(122L);
        treeNodeLink_122.setRuleLimitType(4);
        treeNodeLink_122.setRuleLimitValue("25");
        //组装list
        List<TreeNodeLink> treeNodeLinkList_12 = new ArrayList<>();
        treeNodeLinkList_12.add(treeNodeLink_121);
        treeNodeLinkList_12.add(treeNodeLink_122);
        //将连接放到节点中
        treeNode_12.setTreeNodeLinkList(treeNodeLinkList_12);


        // =============================================
        //果实节点
        // 节点：111
        TreeNode treeNode_111 = new TreeNode();
        treeNode_111.setTreeId(10001L);
        treeNode_111.setTreeNodeId(111L);
        treeNode_111.setNodeType(2);
        treeNode_111.setNodeValue("果实A");

        // 节点：112
        TreeNode treeNode_112 = new TreeNode();
        treeNode_112.setTreeId(10001L);
        treeNode_112.setTreeNodeId(112L);
        treeNode_112.setNodeType(2);
        treeNode_112.setNodeValue("果实B");

        // 节点：121
        TreeNode treeNode_121 = new TreeNode();
        treeNode_121.setTreeId(10001L);
        treeNode_121.setTreeNodeId(121L);
        treeNode_121.setNodeType(2);
        treeNode_121.setNodeValue("果实C");

        // 节点：122
        TreeNode treeNode_122 = new TreeNode();
        treeNode_122.setTreeId(10001L);
        treeNode_122.setTreeNodeId(122L);
        treeNode_122.setNodeType(2);
        treeNode_122.setNodeValue("果实D");


        //树根
        TreeRoot treeRoot = new TreeRoot();
        treeRoot.setTreeId(10001L);
        treeRoot.setTreeRootNodeId(1L);
        treeRoot.setTreeName("规则决策树");

        Map<Long, TreeNode> treeNodeMap = new HashMap<>();
        treeNodeMap.put(1L, treeNode_01);
        treeNodeMap.put(11L, treeNode_11);
        treeNodeMap.put(12L, treeNode_12);
        treeNodeMap.put(111L, treeNode_111);
        treeNodeMap.put(112L, treeNode_112);
        treeNodeMap.put(121L, treeNode_121);
        treeNodeMap.put(122L, treeNode_122);

        TreeRich treeRich = new TreeRich(treeRoot, treeNodeMap);


        log.info("规则树所有信息  {}", JSONUtil.toJsonStr(treeRich));

        IEngine engine = new TreeEngineHandle();
        Map<String, String> decisionMatter = new HashMap<>();
        decisionMatter.put("age", "25");
        decisionMatter.put("gender", "man");

        EngineResult result = engine.process(10001L, "shiguiwu", treeRich, decisionMatter);

        log.info("测试结果：{}", JSONUtil.toJsonStr(result));

        return null;
    }

}
